package com.jsyl.lwbk.web.controller.business;

import com.jsyl.lwbk.business.dto.SaveProjectDTO;
import com.jsyl.lwbk.business.dto.TrainPackageProjectDTO;
import com.jsyl.lwbk.business.service.TrainPackageProjectService;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import com.jsyl.lwbk.common.core.domain.Ztree;
import com.jsyl.lwbk.common.core.page.TableDataInfo;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 训练包
 */
@Controller
@RequestMapping("/business/trainPackageProject")
public class TrainPackageProjectController extends BaseController {

    private String prefix = "business/trainPackage";

    @Autowired
    TrainPackageProjectService trainPackageProjectService;


    @GetMapping("/list/{trainPackageId}")
    public String child(@PathVariable("trainPackageId") String trainPackageId,ModelMap mmap) {
        mmap.put("trainPackageId", trainPackageId);
        return prefix + "/trainPackageProjectList";
    }
    /**
     * 页面跳转-新增题库
     */
    @GetMapping("/add/{trainPackageId}")
    public String add(@PathVariable("trainPackageId") String trainPackageId,ModelMap mmap) {
        mmap.put("trainPackageId", trainPackageId);
        return prefix + "/addTrainPackageProject";
    }
    /**
     * 页面跳转-训练包选择项目
     *
     * @param trainPackageProjectId
     * @param mmap
     * @return
     */
    @GetMapping("/checkTrainProject/{trainPackageProjectId}")
    public String checkProject(@PathVariable("trainPackageProjectId") String trainPackageProjectId, ModelMap mmap) {
        mmap.put("trainPackageProjectId", trainPackageProjectId);
        return prefix + "/checkTrainProject";
    }



    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findList(TrainPackageProjectDTO dto) {
        startPage();
        List<TrainPackageProjectDTO> list =new ArrayList<>();
        if(dto.getTrainPackageId()==null || dto.getTrainPackageId()==""){
            return getDataTable(list);
        }else {
             list= trainPackageProjectService.findList(Long.valueOf(dto.getTrainPackageId()));
        }
        return getDataTable(list);
    }
    @PostMapping("/listByCourse")
    @ResponseBody
    public TableDataInfo listByCourse(TrainPackageProjectDTO dto) {
        startPage();
        List<TrainPackageProjectDTO> list =new ArrayList<>();
        if(dto.getTrainPackageId()==null || dto.getTrainPackageId()==""){
            return getDataTable(list);
        }else {
            list= trainPackageProjectService.listByCourse(Long.valueOf(dto.getTrainPackageId()),Long.valueOf(dto.getChildCourseId()));
        }
        return getDataTable(list);
    }

    /**
     * 新增
     *
     * @param
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addClassPlan(TrainPackageProjectDTO dto) {
        dto.setCreateUser(ShiroUtils.getLoginName());
        trainPackageProjectService.addSubject(dto);
        return success();
    }

    /**
     * 保存训练包选择的项目
     *
     * @param dto
     * @return
     */
    @PostMapping("/saveProject")
    @ResponseBody
    public AjaxResult saveProject(@RequestBody SaveProjectDTO dto) {
        dto.setUpdateUser(ShiroUtils.getLoginName());
        this.trainPackageProjectService.saveProject(dto);
        return success();
    }

    /**
     * 获取选择的项目树
     *
     * @param trainPackageProjectId
     * @return
     */
    @GetMapping("/getProjectTree")
    @ResponseBody
    public List<Ztree> getProjectTree(@RequestParam(required = false) Long trainPackageProjectId) {
        List<Ztree> list = new ArrayList<>();
        list = trainPackageProjectService.projectTree(trainPackageProjectId);
        return list;
    }

    /**
     * 训练包选择的项目调整顺序
     *
     * @param dto
     * @return
     */
    @PostMapping("/sortProject")
    @ResponseBody
    public AjaxResult sortProject(@RequestBody SaveProjectDTO dto) {
        dto.setUpdateUser(ShiroUtils.getLoginName());
        this.trainPackageProjectService.sortProject(dto);
        return success();
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trainPackageProjectService.deleteById(ids));
    }

}
